package com.itfreer.gis.server.tms;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

/**
 * 动态矢量切片服务
 */
@RestController
@RequestMapping("/")
@Api("动态矢量切片服务")
public class RestDTMSService {

	@Autowired(required = false)
	private DTMSServer dtmsServer;

	/**
	 * 获取当前切片
	 * @param response
	 * @param serverID
	 * @param z
	 * @param x
	 * @param y
	 */
	@GetMapping(value = "/dtms/{id:.+}/{z:.+}/{x:.+}/{y:.+}")
	public void generate(HttpServletResponse response, @PathVariable("id") String serverID, @PathVariable("z") int z,
			@PathVariable("x") int x, @PathVariable("y") int y) {
		byte[] result = dtmsServer.generate(serverID, z, x, y);

		if (result == null) {
			result = new byte[] {};
		}
		try {
			response.setHeader("Content-disposition", "application/vnd.mapbox-vector-tile");
			response.setHeader("Cache-Control", "no-cache");
			FileCopyUtils.copy(result, response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
